// --------------------------------------------------------------------------------------------------------------------
// <copyright file="201209080450539_V4.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   The v 4.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Migrations
{
    using System.Data.Entity.Migrations;

    using EcBlog.Data.Sql;
    using EcBlog.Extensions;
    using EcBlog.Models;

    /// <summary>
    /// The v 4.
    /// </summary>
    public partial class V4 : DbMigration
    {
        public override void Up()
        {
            var context = new EcModelContext();
            var unitOfWork = new EcUnitOfWork(context);

            this.Sql(
                "UPDATE Layouts SET LayoutPath = '~/content/themes/default/Views/_Layout.cshtml' WHERE Name ='" + unitOfWork.SiteSettingRepository[SiteSettingKey.DefaultLayoutName] + "'");

            // this.RenameColumn("Users", "Username", "UserName");
            // this.AddColumn("Users", "UserName1", c => c.String(nullable: false, maxLength: 50));
            // this.Sql("Update Users Set UserName1 = Username");
            // this.DropColumn("Users", "Username");
            // this.AddColumn("Users", "UserName", c => c.String(nullable: false, maxLength: 50));
            // this.Sql("Update Users Set UserName = UserName1");
            // this.DropColumn("Users", "UserName1");

            if (context.IsForeignKeyExists("Comments", "PageId"))
            {
                this.DropForeignKey("Comments", "PageId", "Pages");
            }

            if (context.IsForeignKeyExists("Pages", "LayoutId"))
            {
                this.DropForeignKey("Pages", "LayoutId", "Layouts");
            }

            if (context.IsForeignKeyExists("OpenIds", "UserId"))
            {
                this.DropForeignKey("OpenIds", "UserId", "Users");
            }

            if (context.IsIndexExists("Comments", "PageId"))
            {
                this.DropIndex("Comments", new[] { "PageId" });
            }

            if (context.IsIndexExists("Pages", "LayoutId"))
            {
                this.DropIndex("Pages", new[] { "LayoutId" });
            }

            if (context.IsIndexExists("OpenIds", "UserId"))
            {
                this.DropIndex("OpenIds", new[] { "UserId" });
            }

            if (!context.IsColumnExists("CommentHistories", "Operation"))
            {
                this.AddColumn("CommentHistories", "Operation", c => c.Int(nullable: false));
            }

            if (!context.IsColumnExists("LayoutHistories", "Operation"))
            {
                this.AddColumn("LayoutHistories", "Operation", c => c.Int(nullable: false));
            }

            if (!context.IsColumnExists("TagHistories", "Operation"))
            {
                this.AddColumn("TagHistories", "Operation", c => c.Int(nullable: false));
            }

            if (!context.IsColumnExists("UserHistories", "Operation"))
            {
                this.AddColumn("UserHistories", "Operation", c => c.Int(nullable: false));
            }

            if (!context.IsColumnExists("PageHistories", "Operation"))
            {
                this.AddColumn("PageHistories", "Operation", c => c.Int(nullable: false));
            }

            if (!context.IsColumnExists("SiteSettingHistories", "Operation"))
            {
                this.AddColumn("SiteSettingHistories", "Operation", c => c.Int(nullable: false));
            }

            if (context.IsColumnExists("Layouts", "LayoutPath"))
            {
                this.AlterColumn("Layouts", "LayoutPath", c => c.String(nullable: false, maxLength: 100));
            }

            if (context.IsColumnExists("Layouts", "Name"))
            {
                this.AlterColumn("Layouts", "Name", c => c.String(nullable: false, maxLength: 100));
            }

            if (context.IsColumnExists("Pages", "Title"))
            {
                this.AlterColumn("Pages", "Title", c => c.String(nullable: false, maxLength: 500));
            }

            if (context.IsColumnExists("Pages", "Slug"))
            {
                this.AlterColumn("Pages", "Slug", c => c.String(nullable: false, maxLength: 500));
            }

            // if (context.IsColumnExists("Users", "UserName"))
            // {
            //    this.AlterColumn("Users", "UserName", c => c.String(nullable: false, maxLength: 50));
            // }
            if (context.IsColumnExists("SiteSettings", "SettingValue"))
            {
                this.AlterColumn("SiteSettings", "SettingValue", c => c.String(maxLength: 1000));
            }

            // if (context.IsColumnExists("UserHistories", "UserName"))
            // {
            //    this.AlterColumn("UserHistories", "UserName", c => c.String(nullable: false, maxLength: 50));
            // }

            if (context.IsColumnExists("SiteSettingHistories", "SettingValue"))
            {
                this.AlterColumn("SiteSettingHistories", "SettingValue", c => c.String(maxLength: 1000));
            }

            if (context.IsColumnExists("Pages", "LastChanged"))
            {
                this.DropColumn("Pages", "LastChanged");    
            }

            if (context.IsColumnExists("Comments", "LastChanged"))
            {
                this.DropColumn("Comments", "LastChanged");
            }

            if (!context.IsPrimaryKeyExists("Comments"))
            {
                this.AddPrimaryKey("Comments", "CommentId");
            }

            if (!context.IsPrimaryKeyExists("Pages"))
            {
                this.AddPrimaryKey("Pages", "PageId");
            }

            if (!context.IsPrimaryKeyExists("Layouts"))
            {
                this.AddPrimaryKey("Layouts", "LayoutId");
            }

            if (!context.IsPrimaryKeyExists("SiteSettings"))
            {
                this.AddPrimaryKey("SiteSettings", "SiteSettingId");
            }

            if (!context.IsPrimaryKeyExists("Tags"))
            {
                this.AddPrimaryKey("Tags", "TagId");
            }

            if (!context.IsPrimaryKeyExists("Users"))
            {
                this.AddPrimaryKey("Users", "UserId");
            }

            if (!context.IsForeignKeyExists("Comments", "PageId"))
            {
                this.AddForeignKey("Comments", "PageId", "Pages", "PageId", cascadeDelete: true);
            }

            if (!context.IsForeignKeyExists("Pages", "LayoutId"))
            {
                this.AddForeignKey("Pages", "LayoutId", "Layouts", "LayoutId", cascadeDelete: true);
            }

            if (!context.IsIndexExists("Comments", "PageId"))
            {
                this.CreateIndex("Comments", "PageId");
            }

            if (!context.IsIndexExists("Pages", "LayoutId"))
            {
                this.CreateIndex("Pages", "LayoutId");
            }

            if (context.IsColumnExists("Layouts", "CssPath"))
            {
                this.DropColumn("Layouts", "CssPath");
            }
            
            if (context.IsColumnExists("Users", "FullName"))
            {
                this.DropColumn("Users", "FullName");
            }

            if (context.IsColumnExists("Users", "Email"))
            {
                this.DropColumn("Users", "Email");
            }

            if (context.IsColumnExists("Users", "PortalRole_Value"))
            {
                this.DropColumn("Users", "PortalRole_Value");
            }

            if (context.IsColumnExists("CommentHistories", "Operation_Value"))
            {
                this.DropColumn("CommentHistories", "Operation_Value");
            }

            if (context.IsColumnExists("LayoutHistories", "CssPath"))
            {
                this.DropColumn("LayoutHistories", "CssPath");
            }

            if (context.IsColumnExists("LayoutHistories", "Operation_Value"))
            {
                this.DropColumn("LayoutHistories", "Operation_Value");
            }

            if (context.IsColumnExists("TagHistories", "Operation_Value"))
            {
                this.DropColumn("TagHistories", "Operation_Value");
            }

            if (context.IsColumnExists("UserHistories", "FullName"))
            {
                this.DropColumn("UserHistories", "FullName");
            }

            if (context.IsColumnExists("UserHistories", "Email"))
            {
                this.DropColumn("UserHistories", "Email");
            }

            if (context.IsColumnExists("UserHistories", "PortalRole_Value"))
            {
                this.DropColumn("UserHistories", "PortalRole_Value");
            }

            if (context.IsColumnExists("UserHistories", "Operation_Value"))
            {
                this.DropColumn("UserHistories", "Operation_Value");
            }

            if (context.IsColumnExists("PageHistories", "Operation_Value"))
            {
                this.DropColumn("PageHistories", "Operation_Value");
            }

            if (context.IsColumnExists("SiteSettingHistories", "Operation_Value"))
            {
                this.DropColumn("SiteSettingHistories", "Operation_Value");
            }

            if (context.IsTableExists("OpenIds"))
            {
                this.DropTable("OpenIds");
            }
        }

        public override void Down()
        {
            CreateTable(
                "OpenIds",
                c => new
                    {
                        OpenIdId = c.Long(nullable: false, identity: true),
                        UserId = c.Long(nullable: false),
                        OpenIdUrl = c.String(maxLength: 500),
                    })
                .PrimaryKey(t => t.OpenIdId);

            this.AddColumn("SiteSettingHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("PageHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("UserHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("UserHistories", "PortalRole_Value", c => c.String(maxLength: 10));
            this.AddColumn("UserHistories", "Email", c => c.String(nullable: false, maxLength: 100));
            this.AddColumn("UserHistories", "FullName", c => c.String(nullable: false, maxLength: 50));
            this.AddColumn("TagHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("LayoutHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("LayoutHistories", "CssPath", c => c.String(nullable: false, maxLength: 100));
            this.AddColumn("CommentHistories", "Operation_Value", c => c.String(maxLength: 10));
            this.AddColumn("Users", "PortalRole_Value", c => c.String(maxLength: 10));
            this.AddColumn("Users", "Email", c => c.String(nullable: false, maxLength: 100));
            this.AddColumn("Users", "FullName", c => c.String(nullable: false, maxLength: 50));
            this.AddColumn("Layouts", "CssPath", c => c.String(nullable: false, maxLength: 100));
            this.DropIndex("Pages", new[] { "LayoutId" });
            this.DropIndex("Comments", new[] { "PageId" });
            this.DropForeignKey("Pages", "LayoutId", "Layouts");
            this.DropForeignKey("Comments", "PageId", "Pages");
            this.AlterColumn("SiteSettingHistories", "SettingValue", c => c.String(maxLength: 50));
            this.AlterColumn("UserHistories", "Username", c => c.String(nullable: false, maxLength: 50));
            this.AlterColumn("SiteSettings", "SettingValue", c => c.String(maxLength: 50));
            this.AlterColumn("Users", "Username", c => c.String(nullable: false, maxLength: 50));
            this.AlterColumn("Pages", "Slug", c => c.String(nullable: false, maxLength: 255));
            this.AlterColumn("Pages", "Title", c => c.String(nullable: false, maxLength: 255));
            this.AlterColumn("Layouts", "Name", c => c.String(nullable: false, maxLength: 255));
            this.AlterColumn("Layouts", "LayoutPath", c => c.String(nullable: false, maxLength: 500));
            this.DropColumn("SiteSettingHistories", "Operation");
            this.DropColumn("PageHistories", "Operation");
            this.DropColumn("UserHistories", "Operation");
            this.DropColumn("TagHistories", "Operation");
            this.DropColumn("LayoutHistories", "Operation");
            this.DropColumn("CommentHistories", "Operation");
            this.CreateIndex("OpenIds", "UserId");
            this.CreateIndex("Pages", "LayoutId");
            this.CreateIndex("Comments", "PageId");
            this.AddForeignKey("OpenIds", "UserId", "Users", "UserId");
            this.AddForeignKey("Pages", "LayoutId", "Layouts", "LayoutId");
            this.AddForeignKey("Comments", "PageId", "Pages", "PageId");
        }
    }
}
